package com.platform.api;

import com.platform.annotation.LoginUser;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.util.ApiBaseAction;
import com.platform.service.SysDeptService;

import java.util.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

@Api(tags = "邀请码验证")
@RestController
@RequestMapping("/api/invite")
public class ApiInviteController extends ApiBaseAction{
    @Autowired
    private ApiInviteService inviteService;
    @Autowired
    private ApiInviteUserService inviteUserService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private ApiUserDeptService userDeptService;

    @ApiOperation(value="检查邀请码")
    @PostMapping("query")
    @ApiImplicitParams({@ApiImplicitParam(name = "invitation", value = "邀请码", paramType = "path", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "path", required = true)})
    public Object query(String invitation, String userId){

        List<InviteVo> inviteVoList = inviteService.queryByInviteCode(invitation);
        if(inviteVoList != null && inviteVoList.size() == 1) {
            InviteVo inviteVo = inviteVoList.get(0);

            //检查用户是否已经属于指定商家客户，如果是则不要再注册了
            List<String> rootDeptIds = inviteService.queryRootDeptIdsByUserId(userId);

            //用户已经被邀请过
            if(rootDeptIds != null && rootDeptIds.size() > 0){

                //先检查邀请码与用户所属商家是否重叠
                boolean isSame = false;
                for(String rootDeptId : rootDeptIds) {
                    if (inviteVo.getDeptId().equals(rootDeptId)) {
                        isSame = true;
                        break;
                    }
                }

                if(isSame){
                    return toResponsFail("您已经是注册会员，请选择直接登录方式。");
                }
                else{
                    inviteVo.setIsUsed('1');
                    inviteService.update(inviteVo);
                    InviteUserVo inviteUserVo = new InviteUserVo();
                    inviteUserVo.setUserId(Integer.parseInt(userId));
                    inviteUserVo.setInviteId(inviteVo.getId());
                    inviteUserVo.setRegisterDate(new Date());
                    inviteUserService.save(inviteUserVo);

                    return toResponsMsgSuccess("邀请成功");
                }
            }
            //用户从来未被邀请过
            else {
                inviteVo.setIsUsed('1');
                inviteService.update(inviteVo);
                InviteUserVo inviteUserVo = new InviteUserVo();
                inviteUserVo.setUserId(Integer.parseInt(userId));
                inviteUserVo.setInviteId(inviteVo.getId());
                inviteUserVo.setRegisterDate(new Date());
                inviteUserService.save(inviteUserVo);

                return toResponsMsgSuccess("邀请成功");
            }
        }
        else {
            return toResponsFail("您输入的邀请码有误，请联系客服。");
        }
    }

    @ApiOperation(value="检查是否为邀请商家")
    @PostMapping("checkShopKeeper")
    public Object checkShopKeeper(Long rootDeptId, Long userId){
        return toResponsSuccess(inviteService.checkShopKeeper(rootDeptId, userId));
    }

    /**
     * 作者: @author wuf
     * 时间: 2018-12-06
     */
    @ApiOperation(value = "获取收藏的商家列表")
    @PostMapping("getCollectedShops")
    public Object getCollectedShops(@LoginUser UserVo loginUser) {
        Map resultObj = new HashMap();

        //收藏商家列表
        List<InviteUserVo> inviteUserVoList = inviteUserService.queryShopsByUserId(loginUser.getUserId());

        for (InviteUserVo invite :inviteUserVoList) {
            if (invite.getIcon() == null || invite.getIcon() == "") {
            invite.setIcon("/static/images/flag.png");
        }
    }

        resultObj.put("collectedShops", inviteUserVoList);
        return toResponsSuccess(resultObj);
    }

    /**
     * 作者: @author wuf
     * 时间: 2018-12-06
     */
    @ApiOperation(value = "获取自己的店铺")
    @PostMapping("getMyShops")
    public Object getMyShops(@LoginUser UserVo loginUser) {
        Map resultObj = new HashMap();

        //收藏商家列表
        List<InviteUserVo> inviteUserVoList = new ArrayList<InviteUserVo>();

        //自己的店铺列表
        List<UserDeptVo> shops = userDeptService.queryOpenShop(loginUser.getUserId());
        if(shops != null && shops.size() > 0){
            for(UserDeptVo shop : shops){
                InviteUserVo vo = new InviteUserVo();
                vo.setUserId(shop.getShop_user_id().intValue());
                vo.setRootDeptId(shop.getRoot_dept_id());
                vo.setRootDeptName(shop.getRoot_dept_name());
                if (shop.getIcon() == null || shop.getIcon() == "") {
                    vo.setIcon("/static/images/flag.png");
                }
                else {
                    vo.setIcon(shop.getIcon());
                }
                vo.setAppBgColor(shop.getApp_bg_color());
                vo.setAppBgImageUrl(shop.getApp_bg_image_url());
                vo.setAppFontColor(shop.getApp_font_color());
                vo.setRootType(shop.getRoot_type());
                vo.setShopKeeper(true);
                inviteUserVoList.add(vo);
            }
        }

        resultObj.put("myShops", inviteUserVoList);
        return toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "获取商家详细信息")
    @PostMapping("getShopDetail")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptId", value = "商家id", paramType = "path", required = true)})
    public Object getShopDetail(Integer deptId) {
        Map resultObj = new HashMap();
        SysDeptEntity sysDeptEntity=sysDeptService.queryObject(deptId.longValue());
        if (sysDeptEntity.getProxyAgreement() == null || sysDeptEntity.getProxyAgreement() == "") {
            String proxy_agreement = "甲方︰" + sysDeptEntity.getName() + "\n手机︰" + sysDeptEntity.getMobile() + "\n微信︰" + sysDeptEntity.getWechat();
            //String proxy_agreement = "aaaa~";
            sysDeptEntity.setProxyAgreement(proxy_agreement);
        }
        resultObj.put("shopDetail", sysDeptEntity);
        return toResponsSuccess(resultObj);
    }
    @ApiOperation(value = "获取当前店铺")
    @PostMapping("getCurrentShop")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptId", value = "商家id", paramType = "path", required = true)})
    public Object getUserGrade(@LoginUser UserVo loginUser,Long deptId) {
        Map resultObj = new HashMap();
        int flag=0;
        InviteUserVo vo = new InviteUserVo();
        //自己开的店
        List<UserDeptVo> shops = userDeptService.queryOpenShop(loginUser.getUserId());
        for(UserDeptVo shop : shops){
            if(shop.getRoot_dept_id().equals(deptId)) {
                flag=1;
                vo.setUserId(shop.getShop_user_id().intValue());
                vo.setRootDeptId(shop.getRoot_dept_id());
                vo.setRootDeptName(shop.getRoot_dept_name());
                vo.setAppBgColor(shop.getApp_bg_color());
                vo.setAppBgImageUrl(shop.getApp_bg_image_url());
                vo.setAppFontColor(shop.getApp_font_color());
                vo.setRootType(shop.getRoot_type());
                vo.setShopKeeper(true);
                vo.setUserGrade(2);
            }
        }
        if(flag==0) {
            //收藏商家列表
            List<InviteUserVo> inviteUserVoList = inviteUserService.queryShopsByUserId(loginUser.getUserId());
            for(InviteUserVo v:inviteUserVoList){
                if(v.getRootDeptId().equals(deptId)){
                    vo=v;
                }
            }
        }
        resultObj.put("currentShop",vo);
        return toResponsSuccess(resultObj);
    }
}
